package com.han.hotelplat.comm.utils;

import java.util.Random;

/**
 * 产生随机字母，数字，或组合
 * <p>Title: bstp</p>
 * <p>Description: 反编译代码</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: bstp</p>
 * @author liyun
 * @version 1.0
 */
public class RandomStringUtils {
    private static final Random RANDOM = new Random();
    public RandomStringUtils() {
    }

    public static String random(int count) {
        return random(count, false, false);
    }

    public static String randomAscii(int count) {
        return random(count, 32, 127, false, false);
    }

    public static String randomAlphabetic(int count) {
        return random(count, true, false);
    }

    public static String randomAlphanumeric(int count) {
        return random(count, true, true);
    }

    public static String randomNumeric(int count) {
        return random(count, false, true);
    }

    public static String random(int count, boolean letters, boolean numbers) {
        return random(count, 0, 0, letters, numbers);
    }

    public static String random(int count, int start, int end, boolean letters,
                                boolean numbers) {
        return random(count, start, end, letters, numbers, null);
    }

    public static String random(int count, int start, int end, boolean letters,
                                boolean numbers, char set[]) {
        if (start == 0 && end == 0) {
            end = 122;
            start = 32;
            if (!letters && !numbers) {
                start = 0;
                end = 0x7fffffff;
            }
        }
        StringBuffer buffer = new StringBuffer();
        int gap = end - start;
        while (count-- != 0) {
            char ch;
            if (set == null) {
                ch = (char) (RANDOM.nextInt(gap) + start);
            }
            else {
                ch = set[RANDOM.nextInt(gap) + start];
            }
            if (letters && numbers && Character.isLetterOrDigit(ch) ||
                letters && Character.isLetter(ch) ||
                numbers && Character.isDigit(ch) || !letters && !numbers) {
                buffer.append(ch);
            }
            else {
                count++;
            }
        }
        return buffer.toString();
    }

    public static String random(int count, String set) {
        return random(count, set.toCharArray());
    }

    public static char[] azAZ90=new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ,'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; 
    public static String random(int count, char set[]) {
        return random(count, 0, set.length - 1, false, false, set);
    }
}
